// var i;
// var $eventManager = require("./EventManager");
// var $resKeeper = require("./ResKeeper");
// var $evenType = require("./EvenType");
// var $excel = require("./Excel");
// var $gameGlobalVariable = require("./GameGlobalVariable");
// var $buffMag = require("./BuffMag");
// var $uI_MosterLogic = require("./UI_MosterLogic");
// var $uI_Bullet = require("./UI_Bullet");
// var g = cc._decorator;
// var _ = g.ccclass;
// var y = (g.property, g.inspector);
// var m = (function (e) {
//     function t() {
//         var t = (null !== e && e.apply(this, arguments)) || this;
//         t.offtime = 0;
//         t.isbao = !1;
//         return t;
//     }
//     __extends(t, e);
//     t.prototype.onCollisionEnter = function (e, t) {
//         if (new Date().getTime() - this.offtime >= 50) {
//             this.offtime = new Date().getTime();
//             var o = t.node.parent.getComponent($uI_Bullet.default);
//             o.hideBullet();
//             var n = e.node.getComponent($uI_MosterLogic.default);
//             var i = $excel.Excel.shuiguobuff(o.attackfang.sgtianfuid);
//             var a = -1;
//             if (i) {
//                 a = i.buffefftype;
//             }
//             switch (a) {
//                 case 4:
//                     $gameGlobalVariable.GameGlobalVariable.playHitBomb2();
//                     var r = i.value[0];
//                     r += r * $buffMag.BuffMgr.getBombBuffScope(o.attackfang);
//                     var s = o.getNowMosterNode(r);
//                     $eventManager.EventMgr.dispatchEvent(
//                         $evenType.EVENT_TYPE.Game_Add_BaoZha,
//                         o.node.getPosition(),
//                         3,
//                         r
//                     );
//                     n.beAttack(o, !0);
//                     s.forEach(function (e) {
//                         var t = e.getComponent($uI_MosterLogic.default);
//                         if (t.node.uuid != n.node.uuid) {
//                             $buffMag.BuffMgr.mosterBeHit(t, o.attackfang);
//                             var a = (o.getBulletAttack(n) * i.value[1]) / 100;
//                             t.fujiaAttack(a, 1, o);
//                             if ($buffMag.BuffMgr.getMosterBombZengShang() > 0) {
//                                 t.setIsXiGuaBomb();
//                             }
//                         }
//                     });
//                     break;
//                 default:
//                     $buffMag.BuffMgr.mosterBeHit(n, o.attackfang);
//                     n.beAttack(o);
//                     if (this.isbao) {
//                         $gameGlobalVariable.GameGlobalVariable.playHitBomb2();
//                     } else {
//                         $gameGlobalVariable.GameGlobalVariable.playHit();
//                     }
//             }
//         }
//     };
//     t.prototype.onYouHuaPeng = function (e) {
//         if (new Date().getTime() - this.offtime >= 50) {
//             this.offtime = new Date().getTime();
//             var t = this.node.parent.getComponent($uI_Bullet.default);
//             t.hideBullet();
//             var o = e.getComponent($uI_MosterLogic.default);
//             var n = $excel.Excel.shuiguobuff(t.attackfang.sgtianfuid);
//             var i = -1;
//             if (n) {
//                 i = n.buffefftype;
//             }
//             switch (i) {
//                 case 4:
//                     $gameGlobalVariable.GameGlobalVariable.playHitBomb2();
//                     var a = n.value[0];
//                     a += a * $buffMag.BuffMgr.getBombBuffScope(t.attackfang);
//                     var r = t.getNowMosterNode(a);
//                     $eventManager.EventMgr.dispatchEvent(
//                         $evenType.EVENT_TYPE.Game_Add_BaoZha,
//                         t.node.getPosition(),
//                         3,
//                         a
//                     );
//                     o.beAttack(t, !0);
//                     r.forEach(function (e) {
//                         var i = e.getComponent($uI_MosterLogic.default);
//                         if (i.node.uuid != o.node.uuid) {
//                             $buffMag.BuffMgr.mosterBeHit(i, t.attackfang);
//                             var a = (t.getBulletAttack(o) * n.value[1]) / 100;
//                             i.fujiaAttack(a, 1, t);
//                             if ($buffMag.BuffMgr.getMosterBombZengShang() > 0) {
//                                 i.setIsXiGuaBomb();
//                             }
//                         }
//                     });
//                     break;
//                 default:
//                     $buffMag.BuffMgr.mosterBeHit(o, t.attackfang);
//                     o.beAttack(t);
//                     this.fujiaShangHai(t, o);
//                     this.fanweiShangHai(t, o);
//                     if (this.isbao) {
//                         $gameGlobalVariable.GameGlobalVariable.playHitBomb2();
//                     } else {
//                         $gameGlobalVariable.GameGlobalVariable.playHit();
//                     }
//             }
//         }
//     };
//     t.prototype.binDongEwai = function () {};
//     t.prototype.fanweiShangHai = function (e, t) {
//         this.isbao = !1;
//         var o = $buffMag.BuffMgr.getFanweiBuff(e.attackfang);
//         if (o && Math.random() < o.probability / 100) {
//             var n = (e.attack * o.value[0]) / 100;
//             var i = o.value[1];
//             e.getNowMosterNode(i).forEach(function (i) {
//                 var a = i.getComponent($uI_MosterLogic.default);
//                 if (t.node.uuid != a.node.uuid) {
//                     a.fujiaAttack(n, 5, e);
//                     a.setJianShu(o.value[2], o.bufftime, e.attackfang);
//                 }
//             });
//         }
//     };
//     t.prototype.fujiaShangHai = function (e, t) {
//         var o = $buffMag.BuffMgr.getBombBuff(e.attackfang);
//         this.isbao = !1;
//         if (o) {
//             this.isbao = !0;
//             var n = (e.attack * o.value[1]) / 100;
//             var i = o.value[0];
//             e.getNowMosterNode(i).forEach(function (e) {
//                 var o = e.getComponent($uI_MosterLogic.default);
//                 if (t.node.uuid != o.node.uuid) {
//                     o.fujiaAttack(n, 5);
//                 }
//             });
//             var a = 1;
//             if (5 == e.attackfang.playerId) {
//                 a = 3;
//             }
//             $eventManager.EventMgr.dispatchEvent($evenType.EVENT_TYPE.Game_Add_BaoZha, e.node.getPosition(), a, i);
//         }
//     };
//     return __decorate([_], t);
// })($resKeeper.default);
// exports.default = m;


// import { EventMgrInstance } from "./EventManager";
// import ResKeeper from "./ResKeeper";
// import { EVENT_TYPE } from "./EvenType";
// import { Excel } from "./Excel";
// import { GameGlobalVariable } from "./GameGlobalVariable";
// import { BuffMgr } from "./BuffMag";
// import UI_MosterLogic from "./UI_MosterLogic";
// import UI_Bullet from "./UI_Bullet";

// const { ccclass, property, inspector } = cc._decorator;
// @ccclass
// class UI_BulletCollider extends ResKeeper {
//     offtime: number = 0;
//     isbao: boolean = false;

//     onCollisionEnter(e: any, t: any) {
//         if (new Date().getTime() - this.offtime >= 50) {
//             this.offtime = new Date().getTime();
//             const o = t.node.parent.getComponent(UI_Bullet);
//             o.hideBullet();
//             const n = e.node.getComponent(UI_MosterLogic);
//             const i = Excel.shuiguobuff(o.attackfang.sgtianfuid);
//             let a = -1;
//             if (i) {
//                 a = i.buffefftype;
//             }
//             switch (a) {
//                 case 4:
//                     GameGlobalVariable.playHitBomb2();
//                     let r = i.value[0];
//                     r += r * BuffMgr.getBombBuffScope(o.attackfang);
//                     const s = o.getNowMosterNode(r);
//                     EventMgrInstance.dispatchEvent(
//                         EVENT_TYPE.Game_Add_BaoZha,
//                         o.node.getPosition(),
//                         3,
//                         r
//                     );
//                     n.beAttack(o, true);
//                     s.forEach((e: any) => {
//                         const t = e.getComponent(UI_MosterLogic);
//                         if (t.node.uuid != n.node.uuid) {
//                             BuffMgr.mosterBeHit(t, o.attackfang);
//                             const a = (o.getBulletAttack(n) * i.value[1]) / 100;
//                             t.fujiaAttack(a, 1, o);
//                             if (BuffMgr.getMosterBombZengShang() > 0) {
//                                 t.setIsXiGuaBomb();
//                             }
//                         }
//                     });
//                     break;
//                 default:
//                     BuffMgr.mosterBeHit(n, o.attackfang);
//                     n.beAttack(o);
//                     if (this.isbao) {
//                         GameGlobalVariable.playHitBomb2();
//                     } else {
//                         GameGlobalVariable.playHit();
//                     }
//             }
//         }
//     }

//     onYouHuaPeng(e: any) {
//         if (new Date().getTime() - this.offtime >= 50) {
//             this.offtime = new Date().getTime();
//             const t = this.node.parent.getComponent(UI_Bullet);
//             t.hideBullet();
//             const o = e.getComponent(UI_MosterLogic);
//             const n = Excel.shuiguobuff(t.attackfang.sgtianfuid);
//             let i = -1;
//             if (n) {
//                 i = n.buffefftype;
//             }
//             switch (i) {
//                 case 4:
//                     GameGlobalVariable.playHitBomb2();
//                     let a = n.value[0];
//                     a += a * BuffMgr.getBombBuffScope(t.attackfang);
//                     const r = t.getNowMosterNode(a);
//                     EventMgrInstance.dispatchEvent(
//                         EVENT_TYPE.Game_Add_BaoZha,
//                         t.node.getPosition(),
//                         3,
//                         a
//                     );
//                     o.beAttack(t, true);
//                     r.forEach((e: any) => {
//                         const i = e.getComponent(UI_MosterLogic);
//                         if (i.node.uuid != o.node.uuid) {
//                             BuffMgr.mosterBeHit(i, t.attackfang);
//                             const a = (t.getBulletAttack(o) * n.value[1]) / 100;
//                             i.fujiaAttack(a, 1, t);
//                             if (BuffMgr.getMosterBombZengShang() > 0) {
//                                 i.setIsXiGuaBomb();
//                             }
//                         }
//                     });
//                     break;
//                 default:
//                     BuffMgr.mosterBeHit(o, t.attackfang);
//                     o.beAttack(t);
//                     this.fujiaShangHai(t, o);
//                     this.fanweiShangHai(t, o);
//                     if (this.isbao) {
//                         GameGlobalVariable.playHitBomb2();
//                     } else {
//                         GameGlobalVariable.playHit();
//                     }
//             }
//         }
//     }

//     binDongEwai() { }

//     fanweiShangHai(e: any, t: any) {
//         this.isbao = false;
//         const o = BuffMgr.getFanweiBuff(e.attackfang);
//         if (o && Math.random() < o.probability / 100) {
//             const n = (e.attack * o.value[0]) / 100;
//             const i = o.value[1];
//             e.getNowMosterNode(i).forEach((i: any) => {
//                 const a = i.getComponent(UI_MosterLogic);
//                 if (t.node.uuid != a.node.uuid) {
//                     a.fujiaAttack(n, 5, e);
//                     a.setJianShu(o.value[2], o.bufftime, e.attackfang);
//                 }
//             });
//         }
//     }

//     fujiaShangHai(e: any, t: any) {
//         const o = BuffMgr.getBombBuff(e.attackfang);
//         this.isbao = false;
//         if (o) {
//             this.isbao = true;
//             const n = (e.attack * o.value[1]) / 100;
//             const i = o.value[0];
//             e.getNowMosterNode(i).forEach((e: any) => {
//                 const o = e.getComponent(UI_MosterLogic);
//                 if (t.node.uuid != o.node.uuid) {
//                     o.fujiaAttack(n, 5);
//                 }
//             });
//             let a = 1;
//             if (5 == e.attackfang.playerId) {
//                 a = 3;
//             }
//             EventMgrInstance.dispatchEvent(EVENT_TYPE.Game_Add_BaoZha, e.node.getPosition(), a, i);
//         }
//     }
// }

// export default UI_BulletCollider;

import { EventMgrInstance } from "./EventManager";
import ResKeeper from "./ResKeeper";
import { EVENT_TYPE } from "./EvenType";
import { Excel } from "./Excel";
import { GameGlobalVariable } from "./GameGlobalVariable";
import { BuffMgr } from "./BuffMag";
import ViewMosterLogic from "./UI_MosterLogic";
import ViewBullet from "./UI_Bullet";

const { ccclass, property, inspector } = cc._decorator;

/**
 * 建议类名：BulletCollider
 * 子弹碰撞处理类
 */
@ccclass
class ViewBulletCollider extends ResKeeper {
    /** 
     * 上次碰撞的时间 
     */
    private lastCollisionTime: number = 0;
    /** 
     * 是否触发爆炸效果 
     */
    private hasExplosion: boolean = false;

    /**
     * 子弹碰撞回调函数
     * @param otherCollider - 其他碰撞组件
     * @param selfCollider - 自己的碰撞组件
     * 建议方法名：onBulletCollision
     */
    onCollisionEnter(otherCollider: any, selfCollider: any) {
        if (new Date().getTime() - this.lastCollisionTime >= 50) {
            this.lastCollisionTime = new Date().getTime();
            const bulletComp = selfCollider.node.parent.getComponent(ViewBullet);
            bulletComp.hideBullet();
            const monsterComp = otherCollider.node.getComponent(ViewMosterLogic);
            const buffConfig = Excel.shuiguobuff(bulletComp.attackfang.sgtianfuid);
            let buffType = -1;
            if (buffConfig) {
                buffType = buffConfig.buffefftype;
            }
            switch (buffType) {
                case 4:
                    GameGlobalVariable.playBomb2SoundEffect();
                    let buffValue = buffConfig.value[0];
                    buffValue += buffValue * BuffMgr.getBombBuffScope(bulletComp.attackfang);
                    const nearbyMonsters = bulletComp.getNowMosterNode(buffValue);
                    EventMgrInstance.emitEvent(
                        EVENT_TYPE.Game_Add_BaoZha,
                        bulletComp.node.getPosition(),
                        3,
                        buffValue
                    );
                    monsterComp.beAttack(bulletComp, true);
                    nearbyMonsters.forEach((monsterNode: any) => {
                        const nearbyMonsterComp = monsterNode.getComponent(ViewMosterLogic);
                        if (nearbyMonsterComp.node.uuid != monsterComp.node.uuid) {
                            BuffMgr.monsterHit(nearbyMonsterComp, bulletComp.attackfang);
                            const extraDamage = (bulletComp.getBulletAttack(monsterComp) * buffConfig.value[1]) / 100;
                            nearbyMonsterComp.fujiaAttack(extraDamage, 1, bulletComp);
                            if (BuffMgr.getMonsterBombDamageIncrease() > 0) {
                                nearbyMonsterComp.setIsXiGuaBomb();
                            }
                        }
                    });
                    break;
                default:
                    BuffMgr.monsterHit(monsterComp, bulletComp.attackfang);
                    monsterComp.beAttack(bulletComp);
                    if (this.hasExplosion) {
                        GameGlobalVariable.playBomb2SoundEffect();
                    } else {
                        GameGlobalVariable.playHitSound();
                    }
            }
        }
    }

    /**
     * 优化的碰撞回调函数
     * @param otherCollider - 其他碰撞组件
     * 建议方法名：onOptimizedCollision
     */
    onOptimizedCollision(otherCollider: any) {
        if (new Date().getTime() - this.lastCollisionTime >= 50) {
            this.lastCollisionTime = new Date().getTime();
            const bulletComp = this.node.parent.getComponent(ViewBullet);
            bulletComp.hideBullet();
            const monsterComp:ViewMosterLogic = otherCollider.getComponent(ViewMosterLogic);
            const buffConfig = Excel.shuiguobuff(bulletComp.attackfang.sgtianfuid);
            let buffType = -1;
            if (buffConfig) {
                buffType = buffConfig.buffefftype;
            }
            switch (buffType) {
                case 4:
                    GameGlobalVariable.playBomb2SoundEffect();
                    let buffValue = buffConfig.value[0];
                    buffValue += buffValue * BuffMgr.getBombBuffScope(bulletComp.attackfang);
                    const nearbyMonsters = bulletComp.getCurrentMonsterNodes(buffValue);
                    EventMgrInstance.emitEvent(
                        EVENT_TYPE.Game_Add_BaoZha,
                        bulletComp.node.getPosition(),
                        3,
                        buffValue
                    );
                    monsterComp.receiveAttack(bulletComp, true);
                    nearbyMonsters.forEach((monsterNode: any) => {
                        const nearbyMonsterComp = monsterNode.getComponent(ViewMosterLogic);
                        if (nearbyMonsterComp.node.uuid != monsterComp.node.uuid) {
                            BuffMgr.monsterHit(nearbyMonsterComp, bulletComp.attackfang);
                            const extraDamage = (bulletComp.getBulletAttack(monsterComp) * buffConfig.value[1]) / 100;
                            nearbyMonsterComp.fujiaAttack(extraDamage, 1, bulletComp);
                            if (BuffMgr.getMonsterBombDamageIncrease() > 0) {
                                nearbyMonsterComp.setIsXiGuaBomb();
                            }
                        }
                    });
                    break;
                default:
                    BuffMgr.monsterHit(monsterComp, bulletComp.attackfang);
                    monsterComp.receiveAttack(bulletComp);
                    this.applyExtraDamage(bulletComp, monsterComp);
                    this.applyAreaDamage(bulletComp, monsterComp);
                    if (this.hasExplosion) {
                        GameGlobalVariable.playBomb2SoundEffect();
                    } else {
                        GameGlobalVariable.playHitSound();
                    }
            }
        }
    }

    /**
     * 暂时未使用的保留方法
     * 建议方法名：unusedMethod
     */
    binDongEwai() { }

    /**
     * 应用范围伤害
     * @param bulletComp - 子弹组件
     * @param monsterComp - 怪物组件
     * 建议方法名：applyAreaDamage
     */
    applyAreaDamage(bulletComp: any, monsterComp: any) {
        this.hasExplosion = false;
        const buff = BuffMgr.getRangeBuff(bulletComp.attackfang);
        if (buff && Math.random() < buff.probability / 100) {
            const damage = (bulletComp.attack * buff.value[0]) / 100;
            const radius = buff.value[1];
            bulletComp.getNowMosterNode(radius).forEach((node: any) => {
                const nearbyMonsterComp = node.getComponent(ViewMosterLogic);
                if (monsterComp.node.uuid != nearbyMonsterComp.node.uuid) {
                    nearbyMonsterComp.fujiaAttack(damage, 5, bulletComp);
                    nearbyMonsterComp.setJianShu(buff.value[2], buff.bufftime, bulletComp.attackfang);
                }
            });
        }
    }

    /**
     * 应用附加伤害
     * @param bulletComp - 子弹组件
     * @param monsterComp - 怪物组件
     * 建议方法名：applyExtraDamage
     */
    applyExtraDamage(bulletComp: any, monsterComp: any) {
        const buff = BuffMgr.getBombBuff(bulletComp.attackfang);
        this.hasExplosion = false;
        if (buff) {
            this.hasExplosion = true;
            const damage = (bulletComp.attack * buff.value[1]) / 100;
            const radius = buff.value[0];
            bulletComp.getNowMosterNode(radius).forEach((node: any) => {
                const nearbyMonsterComp = node.getComponent(ViewMosterLogic);
                if (monsterComp.node.uuid != nearbyMonsterComp.node.uuid) {
                    nearbyMonsterComp.fujiaAttack(damage, 5);
                }
            });
            let eventType = 1;
            if (5 == bulletComp.attackfang.playerId) {
                eventType = 3;
            }
            EventMgrInstance.emitEvent(EVENT_TYPE.Game_Add_BaoZha, bulletComp.node.getPosition(), eventType, radius);
        }
    }
}

export default ViewBulletCollider;